package us.ajg0702.leaderboards.libs.hikari;

import us.ajg0702.leaderboards.libs.hikari.util.PropertyElf;

import javax.naming.*;
import javax.naming.spi.ObjectFactory;
import javax.sql.DataSource;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;


public class HikariJNDIFactory
        implements ObjectFactory {
    public synchronized Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
        if (obj instanceof Reference && "javax.sql.DataSource" .equals(((Reference) obj).getClassName())) {
            Reference ref = (Reference) obj;
            Set<String> hikariPropSet = PropertyElf.getPropertyNames(HikariConfig.class);

            Properties properties = new Properties();
            Enumeration<RefAddr> enumeration = ref.getAll();
            while (enumeration.hasMoreElements()) {
                RefAddr element = enumeration.nextElement();
                String type = element.getType();
                if (type.startsWith("dataSource.") || hikariPropSet.contains(type)) {
                    properties.setProperty(type, element.getContent().toString());
                }
            }
            return createDataSource(properties, nameCtx);
        }
        return null;
    }


    private DataSource createDataSource(Properties properties, Context context) throws NamingException {
        String jndiName = properties.getProperty("dataSourceJNDI");
        if (jndiName != null) {
            return lookupJndiDataSource(properties, context, jndiName);
        }

        return new HikariDataSource(new HikariConfig(properties));
    }


    private DataSource lookupJndiDataSource(Properties properties, Context context, String jndiName) throws NamingException {
        if (context == null) {
            throw new RuntimeException("JNDI context does not found for dataSourceJNDI : " + jndiName);
        }

        DataSource jndiDS = (DataSource) context.lookup(jndiName);
        if (jndiDS == null) {
            Context ic = new InitialContext();
            jndiDS = (DataSource) ic.lookup(jndiName);
            ic.close();
        }

        if (jndiDS != null) {
            HikariConfig config = new HikariConfig(properties);
            config.setDataSource(jndiDS);
            return new HikariDataSource(config);
        }

        return null;
    }
}


*Location:D:\User\Xuan\Downloads\ajLeaderboards-1.3.3.jar\\us\ajg0702\leaderboards\libs\hikari\HikariJNDIFactory.class
 *Java compiler version:8(52.0)
         *JD-Core Version:1.1.3
